package org.example.ui;

import org.example.Client;

import javax.swing.*;
import java.awt.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class ClientUI extends JFrame {

    private JTextArea logArea;
    private JButton startButton;
    private JButton stopButton;
    private JTextField ipField;
    private JTextField portField;

    private ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor();
    private Future<?> connectionFuture;
    private Future<?> broadcastFuture;
    private Client.ConnectionServer connectionServer;
    private Client.Broadcast broadcast;

    public ClientUI() {
        setTitle("War3 Connection Client");
        setSize(600, 450);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // 输入框
        JPanel inputPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        inputPanel.add(new JLabel("目标IP:"));
        ipField = new JTextField("120.34.0.1", 12);
        inputPanel.add(ipField);

        inputPanel.add(new JLabel("端口:"));
        portField = new JTextField("8888", 6);
        inputPanel.add(portField);

        // 日志区域
        logArea = new JTextArea();
        logArea.setEditable(false);
        JScrollPane scrollPane = new JScrollPane(logArea);

        // 按钮
        startButton = new JButton("启动连接");
        stopButton = new JButton("停止连接");
        stopButton.setEnabled(false);

        JPanel buttonPanel = new JPanel();
        buttonPanel.add(startButton);
        buttonPanel.add(stopButton);

        // 布局
        setLayout(new BorderLayout());
        add(inputPanel, BorderLayout.NORTH);
        add(scrollPane, BorderLayout.CENTER);
        add(buttonPanel, BorderLayout.SOUTH);

        // 事件绑定
        startButton.addActionListener(e -> startConnection());
        stopButton.addActionListener(e -> stopConnection());
    }

    private void startConnection() {
        try {
            String ip = ipField.getText().trim();
            int port = Integer.parseInt(portField.getText().trim());

            // 设置目标地址和端口
            Client.serverIp = ip;
            Client.serverPort = port;

            connectionServer = new Client.ConnectionServer();
            broadcast = new Client.Broadcast();
            broadcast.connectionServer = connectionServer;

            connectionFuture = executor.submit(connectionServer);
            broadcastFuture = executor.submit(broadcast);

            log("连接已启动 -> " + ip + ":" + port);
            startButton.setEnabled(false);
            stopButton.setEnabled(true);

        } catch (Exception ex) {
            log("启动失败: " + ex.getMessage());
            ex.printStackTrace();
        }
    }

    private void stopConnection() {
        try {
            if (connectionFuture != null) connectionFuture.cancel(true);
            if (broadcastFuture != null) broadcastFuture.cancel(true);

            log("连接已停止");
            startButton.setEnabled(true);
            stopButton.setEnabled(false);

        } catch (Exception ex) {
            log("停止失败: " + ex.getMessage());
        }
    }

    private void log(String message) {
        SwingUtilities.invokeLater(() -> {
            logArea.append(message + "\n");
        });
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            ClientUI ui = new ClientUI();
            ui.setVisible(true);
        });
    }
}
